package com.ebt.m.view.timeline;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.customer.net.json.CustomerDynamicJson;
import com.ebt.m.utils.DateUtils;

import java.util.List;

public class TimeLineRecyclerAdapter2 extends RecyclerView.Adapter {
    List<CustomerDynamicJson> list;
    Context context;
    protected ISectionFactory factory;
    private View emptyView;
    private int contentHeight;
    private int emptyHeight;
    private int emptyWidth;
    private int mChoiceMode;


    public TimeLineRecyclerAdapter2(Context context, List<CustomerDynamicJson> list, ISectionFactory factory) {
        this.list = list;
        this.context = context;
        this.factory = factory;
    }

    public void setEmptyView(View view) {
        emptyView = view;
    }

    public void setData(List<CustomerDynamicJson> list) {
        this.list = list;
        //添加时间分组
        groupTime(this.list);
        notifyData();
    }

    private List<CustomerDynamicJson> groupTime(List<CustomerDynamicJson> list){
        if (list == null || list.size() == 0){
            return list;
        }
        String prev = null;
        String curr = null;
        for (CustomerDynamicJson json : list) {
            curr = DateUtils.dateTime2String(DateUtils.parseTimestampInSecondToDate(json.activeTime), DateUtils.YYYY_MM_DD);
            if (curr != null && !curr.equals(prev)){
                json.showDate = true;
            }else{
                json.showDate = false;
            }
            prev = curr;
        }
        return list;
    }

    public void setChoiceMode(int choiceMode) {
        this.mChoiceMode = choiceMode;
    }

    public void setDataAndNotify(List<CustomerDynamicJson> list) {
        this.list = list;
        if (emptyView != null) {
            if (this.list != null && this.list.size() > 0) {
                emptyView.setVisibility(View.GONE);
            } else {
                emptyView.setVisibility(View.VISIBLE);
            }
        }
        this.notifyDataSetChanged();
    }

    public void notifyData() {
        if (emptyView != null) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) emptyView.getLayoutParams();
            int delta = contentHeight / 2 - emptyHeight / 2;
            if (delta >= 0) {
                layoutParams.setMargins(0, delta, 0, 0);
            } else {
                layoutParams.height = contentHeight;
                layoutParams.width = contentHeight * emptyWidth / emptyHeight;
            }
            if (this.list != null && this.list.size() > 0) {
                emptyView.setVisibility(View.GONE);
            } else {
                emptyView.setVisibility(View.VISIBLE);
            }
            super.notifyDataSetChanged();
        }
    }

    public CustomerDynamicJson getItem(int position) {
        if (this.list != null && this.list.size() > position) {
            return this.list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        IView view = factory.createView(viewType);
        return new TimeLineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        IView view = (IView) holder.itemView;
        if (view instanceof IDynamicJson){
            ((IDynamicJson)view).updateByJson(getItem(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).activeType;
    }

    public void setEmptyData(int contentHeight, int mEmptyWidth, int mEmptyHeight) {
        this.contentHeight = contentHeight;
        this.emptyHeight = mEmptyHeight;
        this.emptyWidth = mEmptyWidth;
    }

    private class TimeLineViewHolder extends RecyclerView.ViewHolder {

        public TimeLineViewHolder(View itemView) {
            super(itemView);
        }
    }
}
